package dk.modified.reshary;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

	private static final int[] EMPTY_STATE_SET = {};
	private static final int[] GROUP_EXPANDED_STATE_SET = { android.R.attr.state_expanded };
	private static final int[][] GROUP_STATE_SETS = { EMPTY_STATE_SET, // 0
			GROUP_EXPANDED_STATE_SET // 1
	};

	private Context _context;

	private ArrayList<Category> categories;

	public ExpandableListAdapter(Context context, ArrayList<Category> categories) {
		this._context = context;
		this.categories = categories;
	}

	@Override
	public Object getChild(int groupPosition, int childPosititon) {
		return this.categories.get(groupPosition).getChildAtPosition(
				childPosititon);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		final CategorySub child = (CategorySub) getChild(groupPosition, childPosition);
		final String childText = child.getCategoryName();

		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) this._context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater
					.inflate(R.layout.drawer_list_item, null);
		}

		TextView txtListChild = (TextView) convertView
				.findViewById(R.id.navlistitem);

		txtListChild.setText(childText);
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return this.categories.get(groupPosition).getChildrenCount();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return categories.get(groupPosition);
	}

	@Override
	public int getGroupCount() {

		try {
			return categories.size();
		} catch (Exception ex) {

			Intent i = new Intent(_context, SplashScreen.class);
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
					| Intent.FLAG_ACTIVITY_CLEAR_TASK);
			_context.startActivity(i);

			return 0;
		}

	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		Category group = (Category) getGroup(groupPosition);
		String headerTitle = group.getCategoryName();
		if (convertView == null) {

			LayoutInflater infalInflater = (LayoutInflater) this._context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(
					R.layout.drawer_list_item_group, null);

		}

		View ind = convertView.findViewById(R.id.explist_indicator);
		if (ind != null) {
			ImageView indicator = (ImageView) ind;
			if (getChildrenCount(groupPosition) == 0) {
				indicator.setVisibility(View.INVISIBLE);
			} else {
				indicator.setVisibility(View.VISIBLE);
				int stateSetIndex = (isExpanded ? 1 : 0);
				Drawable drawable = indicator.getDrawable();
				drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
			}
		}

		TextView lblListHeader = (TextView) convertView
				.findViewById(R.id.navlistitemheader);

		lblListHeader.setTypeface(null, Typeface.BOLD);
		lblListHeader.setText(headerTitle);

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		if (categories.get(groupPosition).getChildrenCount() >= 1)
			return true;
		else
			return false;
	}
}